#  导入相应的库
import pymysql
from config import  config
#  定义函数链接数据库
def connect_database():
    host, user, passwd, db = config.mysql_info()
    #  链接数据库
    db = pymysql.connect(host=host,user=user,passwd=passwd,db=db)
    return db
#创建数据表
def create_table(job_name):
    #链接数据库
    db = connect_database()
    #创建游标，用来执行sql语言
    curses = db.cursor()
    #编写sql语言创建数据表
    sql_create_table = "create table " + job_name + "(job_name varchar(200) NULL,job_address varchar(200) NULL,degree varchar(200) NULL,company_name varchar(200) NULL,job_requirement varchar(5000) NULL,min_salary int(10) NULL,max_salary int(10) NULL,salary int(10) NULL);"
    #执行sql语句
    curses.execute(sql_create_table)



#  执行sql语句
def insert_data(job_list,job_info):
    #  链接数据库
    db = connect_database()
    #  定义一个游标
    cursor = db.cursor()

    #  定义sql语言
    sql_insert = "insert into"+job_list+"(job_name,job_address,degree,company_name, job_requirement,salary,max_salary,min_salary) values('"+job_info[0]+"','"+job_info[1]+"','"+job_info[2]+"','"+job_info[3]+"','"+job_info[4]+"','"+job_info[5]+"','"+job_info[6]+"');"
    # sql = "create table test(job_name varchar(20),company_name varchar(20),salary int(10),job_address varchar(100),degree varchar(100),job_requirement varchar(5000)null,salary int(10) NULL,max_salary int(10) NULL,min_salary int(10)"
    #sql_insert = "insert into test(job_name,company_name,salary) values(1,2,3)"
    #  执行sql语言
    cursor.execute(sql_insert)
    #  推送执行结果到mysql
    db.commit()
    #  cursor.execute(add_keyword_sql)
    #  执行sql语言
    #  cursor.execute(create_table_sql)

    #  定义创建数据表的sql语言
    #create_table_sql = "create table "+job_name+"(job_name varchar(200),company_name varchar(500),job_address varchar(100),degree varchar(100),job_requirement varchar(5000)null,salary int(10) NULL,max_salary int(10) NULL,min_salary int(10));"
    #  定义添加字段的sql语言
    #add_keyword_sql = "alter table "+job_name+"add column salary int(10) NULL"
    #  数据插入的sql语言
    #print(insert_date_sql)
    #insert_date_sql = "insert into" + job_name + "(job_name,company_name,job_address,degree,job_requirement,salary,max_salary,min_salary) values(%s,%s,%s,%s,%s,%s,%s)"

    # cursor.execute(insert_data_sql)
    #cursor.execute(add_keyword_sql)

    #  执行sql语言
    #  cursor.execute(create_table_sql)
# def select_data(job_list):
#     db = connect_database()
#